package com.nyj.config;

import com.nyj.pojo.Configuration;
import com.nyj.pojo.MappedStatement;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.InputStream;
import java.util.List;

/**
 * @author niuyj
 * @date 2020/4/30 21:59
 * @description
 */
public class XMLMapperBuilder {

    private Configuration configuration;

    public XMLMapperBuilder(Configuration configuration) {
        this.configuration = configuration;
    }

    public void parse(InputStream input) {

        SAXReader saxReader = new SAXReader();
        Document document = null;
        try {
            document = saxReader.read(input);
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        Element rootElement = document.getRootElement();
        String namespace = rootElement.attributeValue("namespace");
        List<Element> list = rootElement.selectNodes("//select");
        List<Element> listI = rootElement.selectNodes("//insert");
        List<Element> listU = rootElement.selectNodes("//update");
        List<Element> listD = rootElement.selectNodes("//delete");
        for (Element element : list) {
            String id = element.attributeValue("id");
            String paramterType = element.attributeValue("parameterType");
            String resultType = element.attributeValue("resultType");
            String sql = element.getTextTrim();
            MappedStatement mappedStatement = new MappedStatement();
            mappedStatement.setId(id);
            mappedStatement.setParamterType(paramterType);
            mappedStatement.setResultType(resultType);
            mappedStatement.setSql(sql);
            //key:statementid->namespace.id
            String key =namespace+"."+id;
            configuration.getMappedStatementMap().put(key,mappedStatement);
        }

        for (Element element : listI) {
            String id = element.attributeValue("id");
            String paramterType = element.attributeValue("parameterType");
            String resultType = element.attributeValue("resultType");
            String sql = element.getTextTrim();
            MappedStatement mappedStatement = new MappedStatement();
            mappedStatement.setId(id);
            mappedStatement.setParamterType(paramterType);
            mappedStatement.setResultType(resultType);
            mappedStatement.setSql(sql);
            //key:statementid->namespace.id
            String key =namespace+"."+id;
            configuration.getMappedStatementMap().put(key,mappedStatement);
        }

        for (Element element : listU) {
            String id = element.attributeValue("id");
            String paramterType = element.attributeValue("parameterType");
            String resultType = element.attributeValue("resultType");
            String sql = element.getTextTrim();
            MappedStatement mappedStatement = new MappedStatement();
            mappedStatement.setId(id);
            mappedStatement.setParamterType(paramterType);
            mappedStatement.setResultType(resultType);
            mappedStatement.setSql(sql);
            //key:statementid->namespace.id
            String key =namespace+"."+id;
            configuration.getMappedStatementMap().put(key,mappedStatement);
        }

        for (Element element : listD) {
            String id = element.attributeValue("id");
            String paramterType = element.attributeValue("parameterType");
            String resultType = element.attributeValue("resultType");
            String sql = element.getTextTrim();
            MappedStatement mappedStatement = new MappedStatement();
            mappedStatement.setId(id);
            mappedStatement.setParamterType(paramterType);
            mappedStatement.setResultType(resultType);
            mappedStatement.setSql(sql);
            //key:statementid->namespace.id
            String key =namespace+"."+id;
            configuration.getMappedStatementMap().put(key,mappedStatement);
        }
    }


    //定义
}
